﻿using Newtonsoft.Json.Converters;
using RecognitionForm.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecognitionForm.Handler
{
    public class DataProvider
    {
        #region 获取Dal对象
        protected IStaffInfoDal staffInfoDal;

        protected IRecognitionLogDal RecognitionLogDal;

        public DataProvider()
        {
            staffInfoDal = new StaffInfoDal();
            RecognitionLogDal = new RecognitionLogDal();
        }
        #endregion

        public async Task<int> InsertStaffAsync(StaffInfo model, IDbTransaction transaction = null, int? commandTimeout = null)
        {
            return await staffInfoDal.InsertAsync(model, transaction, commandTimeout);
        }

        public async Task<bool> DeleteStaffAsync(StaffInfo model, IDbTransaction transaction = null, int? commandTimeout = null)
        {
            return await staffInfoDal.DeleteAsync(model, transaction, commandTimeout);
        }

        public async Task<IEnumerable<StaffInfo>> GetAllStaffAsync(string depart)
        {
            return await staffInfoDal.GetStaffInfosByDepart(depart);
        }


        public async Task<int> InsertRecognitionLogAsync(RecognitionLogInfo model, IDbTransaction transaction = null, int? commandTimeout = null)
        {
            return await RecognitionLogDal.InsertAsync(model, transaction, commandTimeout);
        }


        public async Task<int> GetScalarLog(string staffcode, DateTime startTime, DateTime endTime)
        {
            return await RecognitionLogDal.GetScalarLog(staffcode, startTime, endTime);
        }

        public async Task<IEnumerable<dynamic>> GetLastLogs(DateTime startTime, DateTime endTime)
        {
            return await RecognitionLogDal.GetLastLogs(startTime, endTime);
        }


        public async Task<dynamic> GetStaffName(string code)
        {
            dynamic rtnObj = null;

            var staff = await staffInfoDal.GetAsync(code);
            if (staff != null)
            {
                rtnObj = new ExpandoObject();
                rtnObj.name = staff.name;
                rtnObj.depart = staff.depart;
            }
            return rtnObj;
        }

        public async Task<string> SynchronizePerson()
        {
            //var persons = await GetAllStaffAsync();
            dynamic rtnObj = new ExpandoObject();
            rtnObj.sid = "ems.common.person.synchronize";
            rtnObj.sys = "response";
            rtnObj.sn = "329916139999";
            rtnObj.code = "00000000";
            rtnObj.message = "ok";
            rtnObj.mid = new Random().Next(-2147483648, 0x7fffffff).ToString();
            rtnObj.payload = new
            {
                //datafmt = "1",
                //persons = persons,
                //count = persons.Count()
            };

            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return Newtonsoft.Json.JsonConvert.SerializeObject(rtnObj, timeFormat);
        }


        public string Recognition()
        {
            return new
            {
                sid = "bms.boss.recognition.upload",
                sys = "response",
                code = "00000000",
                message = "ok",
                mid = "xxxxxxxx",
                payload = new object()
            }.SerializeObject();
        }
    }
}
